// pages/collect/collect.js
const app = getApp()
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    key1:0,
    key2:0,
    key:0,
    queryValue: '',
    form_data:{type:1,public_id:0,year:'',title:'',token:wx.getStorageSync('token')},
    yearList: [],
    year: '',
    isSelect: false,
    public_id:0,
    token:'',
    list: [
    
  ],

  },
  //查看
  goInfo(e){
    var id =e.currentTarget.dataset.id
    var key =e.currentTarget.dataset.key
    wx.navigateTo({
      url: 'detail?id=' + id+'&key='+key,
    })
  },
  //取消收藏
  onCollect(e){
    let id = e.currentTarget.dataset.id;
    console.log('id',id)
    app.curl(util.web_url.collect,{public_id:id,token:this.data.form_data.token},'GET',false,false,this,getCurrentPages()).then( (res)=>{
      console.log(res.code)
    //  if(res.code == 0){
    //    util.msg('取消收藏')
    //  }else{
    //    util.msg('收藏成功')
    //  }
 
    
    if(res.code == 1){
      util.msg('收藏成功');
    }else{
      util.msg('取消收藏');
    }
    this.onLoad()
   
            var pages = getCurrentPages();
        var currPage = pages[pages.length - 1];   //当前页面
        var prevPage = pages[pages.length - 2];  //上一个页面
        var info = prevPage.data.list
        console.log(info)
        var index= that.data.index;
        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        let key = "list["+index+"].payStatus";	
        console.log(key)
        prevPage.setData({
          [key]:1
        })
        
  })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
 
  
  
util.check_login()
    var token =util.login_id()
    console.log(token)
    this.setData({'form_data.token':token})
    var token = util.login_id()
    // //  console.log(token)
    this.setData({'form_data.token':token})
      var token = getApp().globalData.token;
      //console.log(token)

      this.publicList()
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },go_detail(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/file/detail?id='+id,
    })
  },publicList(){
    var that = this
    this.setData({'form_data.page':1})
    wx.request({
      url: util.web_url.my_article,
      data: this.data.form_data,
      method: 'POST',
      header: {
        'content-type':  'application/x-www-form-urlencoded',
      },
      dataType: 'json',
      success: function (res) {
console.log(res.data.data.data)
that.setData({list:res.data.data.data,last_page:res.data.data.last_page})
      }
  
   })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  
 
  },onPullDownRefresh() {
    console.log(123)
    util.msg('重新加载中','none',1000)
    this.publicList()
    setTimeout(function(){
      wx.stopPullDownRefresh();
    },500)
  },onReachBottom(){
    var that =this
    console.log("上拉加载....");
   var page = that.data.form_data.page+1
   if (that.data.last_page <= this.data.form_data.page) {
    util.msg('已全部加载完毕');
   }
   console.log(page)
   that.setData({'form_data.page':page})
    wx.request({
      url: util.web_url.public_list,
      data: that.data.form_data,
      method: 'POST',
      header: {
        'content-type':  'application/x-www-form-urlencoded',
      },
      dataType: 'json',
      success: function (res) {
// console.log(res.data.data.data)
// that.setData({list:res.data.data.data})
let list = that.data.list.concat(res.data.data.data)
that.setData({
    list: list,
    loading: false,
});
      }
  
   })
},

})